<template>
  <div class="main-menu">
    <div class="logo">
      <img src="@/assets/img/logo.svg" alt="" />
      <div v-show="!collapseFold" class="title">Admin-NingMeng</div>
    </div>
    <div class="menu">
      <el-menu
        active-text-color="#FFF"
        class="el-menu-vertical-demo"
        :default-active="defaultActie"
        text-color="#b7bdc3"
        :collapse="collapseFold"
      >
        <template v-for="item in userMenus" :key="item.id">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <el-icon>
                <component :is="item.icon.split('el-icon-')[1]"></component>
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="iten in item.children" :key="iten.id">
              <el-menu-item :index="iten.id + ''" @click="clickToPath(iten)">
                {{ iten.name }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>
    </div>
  </div>
</template>

<script setup lang="ts">
import useLoginStore from '@/store/modules/login'
import { useRouter, useRoute } from 'vue-router'
import { computed } from 'vue'
import { mapPathMenu } from '@/utils/map-menu'
const router = useRouter()
const route = useRoute()
const loginStore = useLoginStore()
const { userMenus } = loginStore
defineProps({
  collapseFold: {
    type: Boolean,
    default: false
  }
})
const defaultActie = computed(() => {
  const pathMenu = mapPathMenu(route.path, userMenus)
  return pathMenu.id + ''
})
// 跳转路由
function clickToPath(iten: any) {
  router.push(iten.url)
}
</script>
<style lang="less" scoped>
.main-menu {
  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    justify-content: flex-start;
    align-items: center;
    overflow: hidden;
    .title {
      color: #fff;
      font-size: 18px;
    }
    img {
      height: 100%;
      margin: 0 10px;
    }
  }
  .el-menu--collapse {
    .el-menu-item.is-active {
      background-color: var(--el-color-primary);
    }
  }
}
.el-menu {
  border-right: 0;
  .el-menu-item.is-active {
    background-color: var(--el-color-primary);
  }
}
.el-sub-menu {
  transition: all 0.5s !important;
  .el-menu-item {
    padding-left: 50px;
    background-color: var(--el--menu--active--bg--);
  }
  :deep(.el-sub-menu__title) {
    background-color: var(--el--side--bg--);
  }
  .el-menu-item:hover {
    color: #fff;
  }
}
</style>
